package cn.hs.ph.servlet;

import cn.hs.ph.dao.CommunityDao;
import cn.hs.ph.dao.impl.CommunityDaoImpl;
import cn.hs.ph.entity.Community;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/community")
public class CommunityServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.设置编码方式
        //2.读取action，并判断
        String action=req.getParameter("action");
        CommunityDao communityDao=new CommunityDaoImpl();
        //3.根据action执行不同的代码
        if ("selectAll".equals(action)){
            //3.1接收数据
            String pageNum=req.getParameter("pageNum");
            String pageSize=req.getParameter("pageSize");
            String startTime=req.getParameter("startTime");
            String endTime=req.getParameter("endTime");
            String communityName=req.getParameter("communityName");
            //3.2 封装到map集合
            Map<String,String> map=new HashMap();
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("communityName",communityName);
            //3.3 连接数据库操作
            //3.3.1 查询所有的数据
            List<Community> list=new CommunityDaoImpl().selectAll(map);
            //3.3.2 查询总条数
            int total=new CommunityDaoImpl().selectCount(map);
            //3.4 给出响应信息
            Map resultMap=new HashMap();
            resultMap.put("list",list);
            resultMap.put("total",total);
            Result result = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(result));
        }else if ("selectbyid".equals(action)){
            //3.1接收数据
            String id = req.getParameter("communityId");
            int communityId = 0;
            if (id!=null && !"".equals(id)){
                communityId=Integer.parseInt(id);
            }
            //3.3 连接数据库操作
            //查询
            String communityName = new CommunityDaoImpl().selectById(communityId);
            //3.4 给出响应信息
            Map resultMap=new HashMap();
            resultMap.put("communityName",communityName);
            Result result = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(result));
        } else if ("update".equals(action)){  //新增和修改
            //收集数据
            String addOrUpdateFlag = req.getParameter("addOrUpdateFlag");
            String communityAddress = req.getParameter("communityAddress");
            String code = req.getParameter("communityAreaCode");
            int communityAreaCode = 0;
            if (code!=null && !"".equals(code)){
                communityAreaCode=Integer.parseInt(code);
            }
            String communityCity = req.getParameter("communityCity");
            String communityCoordinates = req.getParameter("communityCoordinates");
            String communityDistrict = req.getParameter("communityDistrict");
            String id = req.getParameter("communityId");
            int communityId = 0;
            if (id!=null && !"".equals(id)){
                communityId=Integer.parseInt(id);
            }
            String communityLatitude = req.getParameter("communityLatitude");
            String communityLongitude = req.getParameter("communityLongitude");
            String communityName = req.getParameter("communityName");
            String communityProvince = req.getParameter("communityProvince");
            //组成对象
            Community community=new Community(communityId,communityName,communityProvince,communityCity,communityDistrict,communityAddress,communityLatitude,communityLongitude,communityAreaCode,communityCoordinates);
            //判断是新增还是修改
            if ("1".equals(addOrUpdateFlag)){  //新增
                int num = communityDao.insert(community);
                Result result=null;
                if (num!=0){  //新增成功
                    result = Result.success("新增成功");
                }else {
                    result = Result.error("新增失败");
                }
                resp.getWriter().write(JSON.toJSONString(result));
            }else {  //修改
                int num = communityDao.update(community);
                Result result=null;
                if (num!=0){  //新增成功
                    result = Result.success("修改成功");
                }else {
                    result = Result.error("修改失败");
                }
                resp.getWriter().write(JSON.toJSONString(result));
            }
        }else if ("delete".equals(action)){
            //收集数据
            String id = req.getParameter("communityId");
            int communityId = 0;
            if (id!=null && !"".equals(id)){
                communityId=Integer.parseInt(id);
            }
            int num = communityDao.delete(communityId);
            Result result=null;
            if (num!=0){  //删除
                result = Result.success("删除成功");
            }else {
                result = Result.error("删除失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
